﻿using A.M.ETO;
using A.M.Storage.Application;
using AutoMapper;
using J.M.Inventory.Application.IInventoryServices;
using J.M.Inventory.Domain.DTO;
using J.M.Inventory.Domain.Entity;
using J.M.Inventory.Infrastruecure.IInventoryRepository;
using Q.M.Sell.Doamin.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace J.M.Inventory.Application.InventoryServices
{
    /// <summary>
    /// 仓库表服务层实现
    /// </summary>
    public class WarehouseServices : BaseServices<warehouse, string>, IWarehouseServices
    {
        private readonly IWarehouseRepository warehouseRepository;
        private readonly IMapper mapper;

        public WarehouseServices(IWarehouseRepository warehouseRepository, IMapper mapper) : base(warehouseRepository)
        {
            this.mapper = mapper;
            this.warehouseRepository = warehouseRepository;
        }
        /// <summary>
        /// 仓库树的显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<TreeDTO> GetWarehouse(string id,string? code)
        {
            var list = warehouseRepository.GetShowInfor();
            //if (!string.IsNullOrEmpty(id))
            //{
            //    list = list.Where(x => x.fid == id).ToList();
            //}
            //if (!string.IsNullOrEmpty(code))
            //{
            //    list = list.Where(x => x.warehousecode == code).ToList();
            //}
            var query = from a in list
                            //where a.fid == id && (!string.IsNullOrEmpty(code) || a.warehousecode == code)
                        where a.fid == id
                        select new TreeDTO
                        {
                            label = a.warehousename,
                            value = a.warehousecode.ToString(),
                            children = GetWarehouse(a.warehousecode,code),
                            principalname = a.principalname,
                            remark = a.remark,
                            waerhousestate = a.waerhousestate,
                            warehouseadd = a.warehouseadd,
                            warehousecode=a.warehousecode
                        };
            if (!string.IsNullOrEmpty(code))
            {
                query=query.Where(x => x.label == code);
            }
 
            return query.Count() == 0 ? null : query.ToList();
        }
        /// <summary>
        /// 修改子集
        /// </summary>
        /// <param name="warehouseDTO"></param>
        /// <returns></returns>
        public  int TreeUpd(WarehouseDTO warehouseDTO)
        {
            //实例化这个实体
            warehouse warehouse = new warehouse();
            //使用automapper进行映射
            warehouse= mapper.Map<warehouse>(warehouseDTO);

            var obj = warehouseRepository.GetShowObj(x => x.warehousecode == warehouse.warehousecode);
            obj.fid = warehouse.fid;
            obj.warehousename = warehouse.warehousename;
            return warehouseRepository.UpdInfor(obj);
        }
        /// <summary>
        /// 删除子集
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       public int DeleteTree(string id)
        {
            var number = warehouseRepository.GetShowObj(x => x.warehousecode == id);
            if (number==null)
            {
               return -1;
            }
            return warehouseRepository.DelInfor(number);
        }
        /// <summary>
        /// 添加时候用的树形
        /// </summary>
        /// <returns></returns>
       public List<TreeDTO> AddFromTree()
        {
            var list = warehouseRepository.GetShowInfor();
            var query = from a in list
                        select new TreeDTO
                        {
                            value = a.warehousecode.ToString(),
                            label= a.warehousecode.ToString(),
                        };
            return query.Count() == 0 ? null : query.ToList();
        }
        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="warehouseDTO"></param>
        /// <returns></returns>
       public ApiReturn<string> AddFromWarehouse(WarehouseDTO warehouseDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            warehouse warehouse = new warehouse();
            //使用automapper进行映射
            warehouse = mapper.Map<warehouse>(warehouseDTO);
            var number = warehouseRepository.AddInfor(warehouse);
            if (number > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功";
            }
            return  apiReturn;
        }
        /// <summary>
        /// 反填仓库信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<warehouse> FindWarehouse(string? code)
        {
            Expression<Func<warehouse, bool>> i = c => c. warehousecode== code;
            var warehouses =  warehouseRepository.GetShowObj(i);
            return warehouses;

        }

 
    }
}
